#include "Entity.h"
#include <cassert>
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include <Windows.h>

// Constructor
CEntity::CEntity( void )
{
	// Assign the data members to clean values
	m_nImageID	= -1;
	
	m_nPosX		= 0;
	m_nPosY		= 0;
	
	m_nVelX		= 0;
	m_nVelY		= 0;

	m_unRefCount= 1;

	TypeofObj = Obj_Base;
	m_nActionID = 0;

	
	m_sHealth			= 0;
	m_fAttackSpeed		= 0;
	m_fAttackDamage		= 0;
	m_fMoveSpeed		= 0;
	m_sDefense			= 0;
	m_fRange			= 0;
}

// Destructor
CEntity::~CEntity( void )
{
	/*	do nothing ... yet */
}

// IEntity Interface:
/*virtual*/ void CEntity::Update( float fElapsedTime )
{
	// Adjust the token's position by its velocity (each frame)
	m_nPosX += m_nVelX * fElapsedTime;
	m_nPosY += m_nVelY * fElapsedTime;
}

/*virtual*/ void CEntity::Render( void )
{
	// Sanity-check that the image exists
	assert( m_nImageID != -1 && "CEntity::Render - image does not exist" );


	// Render the token's image using the position
	// as the top-left corner
	CSGD_TextureManager::GetInstance()->Draw(
		m_nImageID, (int)m_nPosX, (int)m_nPosY );
}

/*virtual*/ void CEntity::AddRef( void )
{
	// Increase the reference count (+1 friend!!!)
	m_unRefCount++;
}

/*virtual*/ void CEntity::Release( void )
{
	// Decrease the reference count (-1 friend :(  ... )
	m_unRefCount--;

	// Self-destruct when the count reaches 0
	if( m_unRefCount == 0 )
		delete this;
}

int CEntity::GetType( void )
{
	return TypeofObj;
}

RECT CEntity::GetRect( void )
{
	RECT size;
	size.left = (LONG)(m_nPosX);
	size.top  = (LONG)(m_nPosY);
	size.right = (LONG)(m_nPosX + m_nWidth);
	size.bottom = (LONG)(m_nPosY + m_nHeight);

	return size;
}

bool CEntity::CheckCollision( IEntity * pOther )
{
	RECT mine;
	if( TRUE == IntersectRect( &mine, &GetRect(), &pOther->GetRect() ))
		return true;
	else
		return false;
}